package cn.gd.controller.systemAdministration.uploadTheFile;


import cn.gd.pojo.UploadSettings;
import cn.gd.service.systemAdministration.UploadSettings.UploadSettingsService;
import cn.gd.utils.Result;
import com.google.gson.Gson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("uploadSettings")
@CrossOrigin
public class UploadSettingsController {

    @Autowired
    private UploadSettingsService service;

    /**
     * 查询是否已经设置了上传文件
     * @return
     */
    @GetMapping("/queryUploadSettings")
    public Result<UploadSettings> queryUploadSettings() {
        return Result.ok(service.queryUploadSettings());
    }

    @PutMapping("/updateUploadSettings")
    public Result<Boolean> updateUploadSettings(@RequestBody UploadSettings uploadSettings) {
        // 序列化为 JSON 字符串
        Gson gson = new Gson();
        uploadSettings.setStringAllowedFileTypes(gson.toJson(uploadSettings.getAllowedFileTypes()));
        return Result.ok(service.updateUploadSettings(uploadSettings));
    }

    @PostMapping("/addUploadSettings")
    public Result<Boolean> addUploadSettings(@RequestBody UploadSettings uploadSettings) {
        // 序列化为 JSON 字符串
        Gson gson = new Gson();
        uploadSettings.setStringAllowedFileTypes(gson.toJson(uploadSettings.getAllowedFileTypes()));
        return Result.ok(service.addUploadSettings(uploadSettings));
    }
}
